/*
 *
 */
package org.jiscinvolve.astrodabis.barberry.common.inport;

import java.net.URI;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.*;

import java.util.Iterator;
import java.io.FileInputStream;

import org.springframework.beans.factory.annotation.Autowired;  

import org.jiscinvolve.astrodabis.barberry.common.core.Job;
import org.jiscinvolve.astrodabis.barberry.common.core.Tag;

import org.jiscinvolve.astrodabis.barberry.common.core.CoreTestBase;
import org.jiscinvolve.astrodabis.barberry.common.data.DataTestBase;

import org.jiscinvolve.astrodabis.barberry.common.data.RowTag;
import org.jiscinvolve.astrodabis.barberry.common.data.JoinTag;

import org.jiscinvolve.astrodabis.barberry.common.data.TapRow;
import org.jiscinvolve.astrodabis.barberry.common.data.TapJoin;
import org.jiscinvolve.astrodabis.barberry.common.data.TapTable;
import org.jiscinvolve.astrodabis.barberry.common.data.TapColumn;
import org.jiscinvolve.astrodabis.barberry.common.data.TapService;

/*
 *
 */
public class LargeDataInportTestCase
extends DataTestBase
    {

    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(LargeDataInportTestCase.class);

    protected TapService service ;
    protected TapTable   tableone ;
    protected TapTable   tabletwo ;
    protected TapColumn  columnone ;
    protected TapColumn  columntwo ;

    @Before
    public void before()
        {
        super.before();
        log.debug("LargeDataInportTestCase.before()");
        
        }

    @Test
    public void test000()
    throws Exception
        {

        service = womble().services().create(
            unique(
                URI.create(
                    "ivo://org.astrodabis.test/service/"
                    )
                ),
            "test-service"
            );
        tableone = service.tables().create(
            "test-table-one"
            );
        tabletwo = service.tables().create(
            "test-table-two"
            );
        columnone = tableone.columns().create(
            "test-column-one"
            );
        columntwo = tabletwo.columns().create(
            "test-column-two"
            );

        Job job = account().jobs().create(
            new Job.Bean(
                "job-name",
                "job-text",
                columnone,
                columntwo
                )
            );


/*
 * Step = 10 Loop = 1 (threonine->leucine)
 * Total time: 9 minutes 29 seconds
 * Added named queries
 * Step = 10 Loop = 1 (threonine->leucine)
 * Total time: 9 minutes 4 seconds
 * Step = 10 Loop = 1 (leucine->leucine)
 * Total time: 9 minutes 4 seconds
 *
 * Step = 10 Loop = 10 (threonine->threonine)
 * Total time: 680 minutes 10 seconds
 * 118,036 rows
 *
 *
 *
 */

        int step = 2 ;
        int loop = 1 ;
        
double time  = 0 ;
long   start = 0 ;
long   done  = 0 ;

        for (int i = 0 ; i < step ; i++)
            {
            for (int j = 0 ; j < step ; j++)
                {
                for (int k = 0 ; k < step ; k++)
                    {
                    for (int l = 0 ; l < step ; l++)
                        {
                        for (int m = 0 ; m < loop ; m++)
                            {

                            start = System.nanoTime();

                            TapRow rowone  = columnone.rows().createQuick(
                                "test-row-" + i
                                );

                            TapRow rowtwo = columntwo.rows().createQuick(
                                "test-row-" + j
                                );

                            TapJoin join  = rowone.joins().createQuick(
                                rowtwo
                                );

                            Tag tag = account().tags().createQuick(
                                "test-tag-" + k
                                );

                            JoinTag jointag = join.tags().createQuick(
                                job,
                                tag,
                                new Double(
                                    time
                                    )
                                );

                            done = System.nanoTime();
                            time = (done - start) / (1000) ;

                            log.debug(jointag);

                            }
                        }
                    }
                }
            }
        }
    }

